import {getBriefs} from "@/backend/services/secu-service";
import {format} from "date-fns";
import styles from "./page.module.scss";

type BriefType = {
    secu_code: string,
    secu_name: string,
    brief_title: string,
    brief_reason: string,
    brief_source: string,
    brief_date: string
}


const BriefCard = ({brief}: { brief: BriefType }) => {
    if (!brief) return null;

    const formatedDate = format(brief.brief_date, 'yyyy-MM-dd');
    const formatedToday = format(new Date(), 'yyyy-MM-dd');
    return (
        <div className={styles.briefCard}>
            <p
                style={{
                    color: `${formatedDate === formatedToday ? 'violet' : 'yellow'}`
                }}
            >
                {formatedDate} | {brief.brief_source.toUpperCase()} | {brief.brief_title}
            </p>
        </div>
    );
};


export default async function briefsPage({params}: { params: Promise<{ code: string }> }) {
    const {code} = await params;
    const briefs: BriefType[] = await getBriefs(code) as BriefType[]
    return <div className={styles.briefContainer}>
        {
            briefs.map((b, i) => <BriefCard key={i} brief={b}/>)
        }
    </div>
}